var head = new Vue({
    el: "#head",
    data: {
        userId:userId, //当前登录用户ID
        car:[],         //购物车数组
        total:0,        //购物车总价格
        totalNum:0      //购物车总数量
    },
    methods: {
        // 删除购物车事件
        deleteCar(index){
            let furnitureId = this.car[index].furnitureId;
            axios.get(PATH+"/car/delete?furnitureId="+furnitureId)
                .then((resp)=> {
                    console.log(resp)
                    if (resp.data.code==="00000"){
                        // 减去购物车总价格价格
                        this.total-=(this.car[index].price*this.car[index].number)
                        this.totalNum-=this.car[index].number
                        // 从购物车数组中减去元素
                        this.car.splice(index,1)
                    }
                })
        },
        checkout(){
            if (this.totalNum==0){
                layer.msg("购物车空空的哦")
            }else{
                window.location=PATH+"/toCheckOut"
            }
        }
    },
    mounted(){
        //页面初始化，加载购物车
        if(this.userId != 0){
            axios.get(PATH+"/car/get?userId="+this.userId)
                .then((resp)=> {
                    this.car = resp.data
                    console.log(this.car)
                    // 计算购物车总价格和数量
                    for (let car of this.car){
                        this.total += car.price*car.number
                        this.totalNum+=car.number;
                    }
                })
        }
    },

})